﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Cqrs.Infrastructure;
using Harmonic.CommandHost;
using Harmonic.Domain;
using Harmonic.ReadModel;
using System.Configuration;
using Harmonic.CommandHost.Commands;
using Cqrs.Infrastructure.Database;
using Ninject;

namespace Harmonic.CommandHost
{

    internal class BindModule : Ninject.Modules.NinjectModule
    {
        public override void Load()
        {
            Bind<IEventStore>().To<EventStore.RavenDb>();
            Bind<IEventPublisher>().To<EventPublisher>();     
        }
    }

    public static class ServiceLocator
    {

        public static CommandBus CommandBus = new CommandBus();
        private static IEventPublisher EventPublisher;
        private static IEventStore EventStore;
        private static Repository<Domain.User> Repository;
        private static CommandHandlers CommandHandler;
        private static EventHandlers registerUserEventHandlers = new EventHandlers();

        static ServiceLocator()
        {
            Ninject.IKernel kernel = new StandardKernel(new BindModule());
            EventStore = kernel.Get<IEventStore>();
            ServiceLocator.EventPublisher = EventStore.Publisher;
            Repository = new Repository<Domain.User>(EventStore);
            CommandHandler = new CommandHandlers(Repository);
        }

        public static void Build()
        {

            CommandBus.RegisterHandler<RegisterNewUser>(CommandHandler.Submit);
            CommandBus.RegisterHandler<UpgradeUser>(CommandHandler.Submit);
            CommandBus.RegisterHandler<DeleteUser>(CommandHandler.Submit);
            CommandBus.RegisterHandler<DowngradeUser>(CommandHandler.Submit);

            EventPublisher.RegisterHandler<UserCreated>(registerUserEventHandlers.Handle);
            EventPublisher.RegisterHandler<UserMadePriority>(registerUserEventHandlers.Handle);
            EventPublisher.RegisterHandler<UserDeleted>(registerUserEventHandlers.Handle);
            EventPublisher.RegisterHandler<UserMadeActive>(registerUserEventHandlers.Handle);
        }

        //private static string GetConnectionString()
        //{
        //    System.Configuration.Configuration rootWebConfig =
        //        System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/");
        //    System.Configuration.ConnectionStringSettings connString;
        //    if (rootWebConfig.ConnectionStrings.ConnectionStrings.Count > 0)
        //    {
        //        connString =
        //            rootWebConfig.ConnectionStrings.ConnectionStrings["EventStore"];
        //        if (connString != null)
        //            return connString.ConnectionString;
        //        else
        //            throw new Exception("Connection String not found");
        //    }
        //    else
        //        throw new Exception("Connection String not found");
        //}
    }
}